#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    p_mem_data(nullptr),
    len_mem_data(0)
{
    ui->setupUi(this);

    timer.setInterval(60);
    connect(&timer, SIGNAL(timeout()), this, SLOT(SLOT_timer()));


    im.create(480, 640, CV_8UC3);//CV_8UC(3)

    if( !memshare.init())
    {
        ui->lb_msg->setText("init failed");
        return;
    }

    memshare.getDataLength(len_mem_data);


    p_mem_data = (char*)malloc(len_mem_data*sizeof(char));
    if(p_mem_data == nullptr)
    {
        ui->lb_msg->setText("init failed");
        return;
    }

    timer.start();
}

MainWindow::~MainWindow()
{
    if(p_mem_data != nullptr)
    {
        free(p_mem_data);
        p_mem_data = nullptr;
    }
    memshare.exit();
    delete ui;
}

void MainWindow::SLOT_timer()
{
    static int i = 0;


    if(!memshare.read(p_mem_data))
    {
        ui->lb_msg->setText("read error");
        return;
    }

    ui->lb_msg->setText(QString("%1 read data len:%2").arg(i++).arg(len_mem_data));

    memcpy(im.data, p_mem_data, len_mem_data);
    qimg = QImage((const uchar*)(im.data), im.cols, im.rows, QImage::Format_RGB888).rgbSwapped();
    ui->lb_img->setPixmap( QPixmap::fromImage(qimg) );


}
